package com.pr.makfilm.user.pages.event.list;

import com.pr.makfilm.core.cmp.authorization.UserAuthorities;
import com.pr.makfilm.core.cmp.table.AjaxDataTable;
import com.pr.makfilm.core.cmp.table.DataProvider;
import com.pr.makfilm.core.cmp.table.SelectionChangeCallback;
import com.pr.makfilm.core.cmp.table.column.DateTimeColumn;
import com.pr.makfilm.domain.Event;
import com.pr.makfilm.domain.ObjectStateEnum;
import com.pr.makfilm.domain.filter.EventFilter;
import com.pr.makfilm.domain.filter.OpenSearchDescription;
import com.pr.makfilm.service.IEventService;
import com.pr.makfilm.user.api.UserApiMappings;
import com.pr.makfilm.user.pages.AbstractUserPage;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.annotation.mount.MountPath;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * Created with IntelliJ IDEA.
 * User: Barbara
 * Date: 21.10.13
 * Time: 22:15
 * To change this template use File | Settings | File Templates.
 */
@MountPath(UserApiMappings.EVENT_LIST_PAGE)
@AuthorizeInstantiation(UserAuthorities.USER)
public class EventListPage extends AbstractUserPage
{
   @SpringBean
   private IEventService eventService;

   private AjaxDataTable<Event> table;

   private Event selected;

   @Override
   protected void onInitialize()
   {
      super.onInitialize();
      List<IColumn<Event, String>> columns = new ArrayList<IColumn<Event, String>>();
      columns.add((new DateTimeColumn<Event, String>(
              new ResourceModel("date.time"),
              Event.FIELD_DATE_TIME,
              "MM",
              new Locale("PL")
      )));
      columns.add(new PropertyColumn<Event, String>(
              new ResourceModel("name"),
              Event.FIELD_NAME,
              Event.FIELD_NAME
      ));

      OpenSearchDescription<Event> osd = new OpenSearchDescription<Event>();
      EventFilter filter = new EventFilter();
      filter.setObjectState(ObjectStateEnum.ACTIVE);
      osd.setFilter(filter);
      DataProvider<Event> dataProvider = new DataProvider<Event>(eventService, osd);
      table = new AjaxDataTable<Event>("table", columns, dataProvider);

      table.setSelectionChangeCallback(new SelectionChangeCallback<Event>()
      {
         @Override
         public void onSelectionChange(AjaxRequestTarget target, IModel<Event> model)
         {
            selected = model.getObject();
            refreshTableView(target);
         }
      });
      add(table);
   }

   private void refreshTableView(AjaxRequestTarget target)
   {
      target.add(table);
//      target.add(filterToolbar);
   }
}
